######################### DESCRIPTIVE STATISTICS ############################
#############################################################################
#Name of code file: descriptive_statistics.R
#Purpose: Descriptive Statistics for Aggregated & Dynamic Datad
#Data In: network_tolerance_processed_data.csv, dynamic_network_tolerance_processed_data.csv
#Data Out: Figure 3, Figure 6, Figure A3, Table A3, and Table A4
######################################################################

#Load Packages
library(readr)
library(stargazer)
library(psych)
library(xtable)

#Set Working Directory
setwd("~/Dropbox/egypt_tolerance_wp_replication/")

#Read in Data
data<-read_csv("data/network_tolerance_processed_data.csv")
time_data<-read_csv("data/dynamic_network_tolerance_processed_data.csv")


##########
#Figure 3#
##########

nonelite_diversity<-data$nonelite_diversity
elite_diversity<-data$elite_diversity
nonelite_diversity_mod<-data$nonelite_diversity_mod

pdf(file="plots/figure3.pdf", width = 11, height = 7)
par(mfrow=c(1,3))
hist(elite_diversity, breaks=50, main=" ", xlab="Elite Network Diversity", col="white")
hist(nonelite_diversity, breaks=50, main="", xlab="Non-Elite Network Diversity \n (With Moderates)", col="white")
hist(data$nonelite_diversity_mod, breaks=50, main="", xlab="Non-Elite Network Diversity \n (Without Moderates) ", col="white")
dev.off()


###########
#Figure A3#
###########

elite_friends_islamist<-data$may15islamist_elite_friends
elite_friends_secular<-data$may15secular_elite_friends
nonelite_friends_islamist<-data$may15nonelite_friends_60pct_islamist 
nonelite_friends_secular<-data$may15nonelite_friends_60pct_secular


pdf(file="plots/figureA3.pdf", width = 11, height = 7)
par(mfrow=c(2,2))
hist(elite_friends_islamist, breaks=45, main="Islamist Elite Friends ", xlab="Islamist Elite Friends", col="lightblue")
hist(elite_friends_secular, breaks=45, main="Secular Elite Friends ", xlab="Secular Elite Friends", col="lightblue")
hist(nonelite_friends_islamist, breaks=45, main="Islamist Non-Elite Friends ", xlab="Islamist Non-Elite Friends", col="lightblue")
hist(nonelite_friends_secular, breaks=45, main="Secular Non-Elite Friends ", xlab="Secular Non-Elite Friends", col="lightblue")
dev.off()

##########
#Figure 6#
##########

elite_diversity_change<-time_data$elite_diversity_total_change
nonelite_diversity_change<-time_data$nonelite_diversity_total_change
nonelite_diversity_change_mod<-time_data$nonelite_diversity_total_change_moderate

pdf(file="plots/figure7.pdf", width = 11, height = 7)
par(mfrow=c(1,3))
hist(elite_diversity_change, breaks=50, main="", xlab="Change in Elite Network Diversity", col="white")
hist(nonelite_diversity_change_mod, breaks=50, main="", xlab="Change in Non-Elite Network Diversity (With Moderates)", col="white")
hist(nonelite_diversity_change, breaks=50, main="", xlab="Change in Non-Elite Network Diversity (Without Moderates)", col="white")
dev.off()


##########
#Table A3#
##########

vars<-c("elite_diversity", "nonelite_diversity", "elite_total", "friends_count", "intolerant_total", "twitter_time", "relevant_total", "total")
data2<-data[vars]

stargaze.it <-function(x,y){
  write.csv(x, file = "stargazerit.csv")
  stargaze_prep = read.csv("stargazerit.csv")
  stargazer(stargaze_prep, type=y)
}

stargaze.it(data2,"latex")


##########
#Table A4#
##########
data$ideology[data$secular==1]<-"secular"
data$ideology[data$islamist==1]<-"islamist"
data$ideology[data$moderate==1]<-"moderate"

vars<-c("elite_diversity", "nonelite_diversity", "elite_total", "friends_count", "intolerant_total", "twitter_time", "relevant_total", "total", "ideology")
data2<-data[vars]
des.mat <- describeBy(data2,data2$ideology,mat=TRUE, digits=2)
des.mat<-des.mat[c(2,4,5,6, 7, 10, 11 )]
xtable(des.mat)







